﻿using FluentValidation;
using Mes.Application.Common;
using Mes.Application.DTO.Inputs.Quality;
using Mes.Application.DTO.Results.Quality;
using Mes.Application.Services.Quality;
using Mes.Domain.BoundedContext;
using Microsoft.AspNetCore.Mvc;

namespace Mes.Application.Web.Controllers.Quality
{
    /// <summary>
    /// 采样计划频率控制器
    /// </summary>
    [ApiController]
    [Route("/api/v1")]
    public class SampleFreqController : ControllerBase
    {
        private readonly ISampleFreqService _sampleFreqService;
        private readonly ILogger<SampleFreqController> _logger;
        private readonly IValidator<AddingSampleFreqInput> _addingSampleFreqInputValidator;
        private readonly IValidator<UpdatingSampleFreqInput> _updatingSampleFreqInputValidator;
        private readonly IValidator<GettingSampleFreqsInput> _gettingSampleFreqInputValidator;

        /// <summary>
        /// 采样计划频率控制器
        /// </summary>
        /// <param name="sampleFreqService">采样计划频率服务</param>
        /// <param name="logger">日志服务</param>
        /// <param name="addingSampleFreqInputValidator">添加采样计划频率验证器</param>
        /// <param name="updatingSampleFreqInputValidator">编辑采样计划频率验证器</param>
        /// <param name="gettingSampleFreqInputValidator">查询采样计划频率验证器</param>
        public SampleFreqController(ISampleFreqService sampleFreqService, ILogger<SampleFreqController> logger, IValidator<AddingSampleFreqInput> addingSampleFreqInputValidator, IValidator<UpdatingSampleFreqInput> updatingSampleFreqInputValidator, IValidator<GettingSampleFreqsInput> gettingSampleFreqInputValidator)
        {
            _sampleFreqService = sampleFreqService;
            _logger = logger;
            _addingSampleFreqInputValidator = addingSampleFreqInputValidator;
            _updatingSampleFreqInputValidator = updatingSampleFreqInputValidator;
            _gettingSampleFreqInputValidator = gettingSampleFreqInputValidator;
        }

        /// <summary>
        /// 新增采样计划频率
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("freq")]
        public async Task<ActionResult> AddSampleFreqAsync([FromBody] AddingSampleFreqInput input)
        {
            try
            {
                var result = await _addingSampleFreqInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                await _sampleFreqService.AddSampleFreqAsync(input);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message, ex);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 删除采样计划频率
        /// </summary>
        /// <param name="freq_id">采样计划频率Id</param>
        /// <returns></returns>
        [HttpDelete("freq")]
        public async Task<ActionResult> DeleteSampleFreqAsync(int freq_id)
        {
            try
            {
                await _sampleFreqService.DeleteSampleFreqAsync(freq_id);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message, ex);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 编辑采样计划频率
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPatch("freq")]
        public async Task<ActionResult> UpdateSampleFreqAsync([FromBody] UpdatingSampleFreqInput input)
        {
            try
            {
                var result = await _updatingSampleFreqInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                await _sampleFreqService.UpdateSampleFreqAsync(input);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message, ex);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 查询采样计划频率实体
        /// </summary>
        /// <param name="freq_id">采样计划频率Id</param>
        /// <returns></returns>
        [HttpGet("freq")]
        public async Task<ActionResult<SampleFreqDetailResult>> GetSampleFreqAsync(int freq_id)
        {
            try
            {

                var response = await _sampleFreqService.GetSampleFreqAsync(freq_id);
                return Ok(response);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message, ex);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 查询采样计划频率分页列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("freqs")]
        public async Task<ActionResult<PagedItemsData<SampleFreqResult>>> GetSampleFreqsAsync([FromQuery] GettingSampleFreqsInput input)
        {
            try
            {
                var result = await _gettingSampleFreqInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                var response = await _sampleFreqService.GetSampleFreqsAsync(input);
                return Ok(response);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message, ex);
                return BadRequest(ex.Message);
            }
        }
    }
}
